<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.List"%>
<%@ page import="org.dualr.litelog.entity.Link"%>
<%@ page import="org.dualr.litelog.dao.LinkDao"%>
<%@ page import="org.dualr.litelog.entity.Category"%>
<%@ page import="org.dualr.litelog.dao.CategoryDao"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Link - LiteLog Administration</title>
<link href="../admin/common.css" rel="stylesheet" type="text/css" media="screen"/>
<script type="text/javaScript">
	function edit(id){
		document.getElementById("link_id").value = id;
		document.getElementById("link_name").value = document.getElementById("link_"+id+"_name").innerHTML;
		document.getElementById("link_url").value = document.getElementById("link_"+id+"_url").innerHTML;
		document.getElementById("link_title").value = document.getElementById("link_"+id+"_title").innerHTML;
		document.getElementById("link_image").value = document.getElementById("link_"+id+"_image").innerHTML;
		document.getElementById("link_categoryId").value = document.getElementById("link_"+id+"_categoryId").innerHTML;
		
	}
	function link_form_submit(){
		var name = document.getElementById("link_form_name");
		var url = document.getElementById("link_form_url");
		if (name.value == ""){
			alert("请填链接名");
			name.focus();
			return;
		}
		if (url.value == ""){
			alert("请填链接地址");
			url.focus();
			return;
		}
		 document.link_form.submit();
	}
	
	function edit_form_submit(){
		var id = document.getElementById("link_id").value;
		if (id == ""){
			alert("请选择要编辑的链接");
			return;
			}
		var name = document.getElementById("link_name");
		var url = document.getElementById("link_url");
		if (name.value == ""){
			alert("请填链接名");
			name.focus();
			return;
		}
		if (url.value == ""){
			alert("请填链接地址");
			url.focus();
			return;
		}
		 document.edit_form.submit();
	}
</script>
</head>
<body>
<h1>链接管理</h1>
<%
	List<Category> categoryList = CategoryDao.getInstance().getListByProperty("type","link","string");
%>
<form action="../admin?entity=link&option=add" method="post" name="link_form">
		名称:<input type="text" name="name" id="link_form_name"></input>
		地址:<input type="text" name="url" id="link_form_url"></input>
		title:<input type="text" name="title" ></input>
		图片:<input type="text" name="image"></input>
		类别:<select name="categoryId">
				<%
					if (categoryList.size() != 0){
								for(Category category : categoryList){
				%>
							<option value="<%=category.getId()%>"><%=category.getName()%></option>
							<%
								}
											
										}
							%>
			</select>
		<input type="button" value="添加" onclick="link_form_submit()"></input>
	</form>
	
	<form action="../admin?entity=link&option=update" method="post" name="edit_form">
		<input type="hidden" name="id" value="" id="link_id"/>
		名称:<input type="text" name="name" id="link_name"></input>
		地址:<input type="text" name="url" id="link_url"></input>
		title:<input type="text" name="title" id="link_title"></input>
		图片:<input type="text" name="image" id="link_image"></input>
		类别:<select name="categoryId" id="link_categoryId">
				<%
					if (categoryList.size() != 0){
								for(Category category : categoryList){
				%>
							<option value="<%=category.getId()%>"><%=category.getName()%></option>
							<%
						}
						
					}
				%>
			</select>
		<input type="button" value="保存" onclick="edit_form_submit()"></input>
	</form>
	<div>
			<table  class="data-list" width="100%" cellspacing="0" cellpadding="0">
				<tr class="caption">
					<td>链接地址</td>
					<td>链接名称</td>
					<td>链接title</td>
					<td>图片</td>
					<td>分类</td>
					<td width="30">编辑</td>
					<td width="30">删除</td>
				</tr>
	<%
		List<Link> articleList = LinkDao.getInstance().getAll();
		if (articleList != null){
			for(Link link : articleList){
				%>
				<tr>
					<td id="link_<%=link.getId()%>_url"><%=link.getUrl()%></td>
					<td id="link_<%=link.getId()%>_name"><%=link.getName()%></td>
					<td id="link_<%=link.getId()%>_title"><%=link.getTitle()%></td>
					<td id="link_<%=link.getId()%>_image"><%=link.getImage()%></td>
					<td id="link_<%=link.getId()%>_categoryId"><%=link.getCategoryId()%></td>
					<td><a href="javascript:edit('<%=link.getId()%>')">编辑</a></td>
					<td><a href="/admin?entity=link&option=delete&id=<%=link.getId()%>&categoryId=<%=link.getCategoryId()%>">删除</a></td>
				</tr>
				<%
			}
		}else{
			%>
			<tr>
				<td colspan="7" align="center">列表为空</td>
			</tr>
			<%
		}
	%>
			</table>
	</div>
</body>
</html>